package com.wy.leetCode;

import org.junit.Test;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @author HelloWorld
 * @create 2021/9/27 9:43
 * @email 154803771@qq.com
 */
public class ComArray {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int[] array = new int[N];
        for (int i = 0; i < N; i++) {
            array[i] = scanner.nextInt();
        }

        Arrays.sort(array);

        int m, n;
        n = (int) Math.sqrt(N);
        m = N / n;
        while (m * n != N) {
            // d
            n = n - 1;
            m = N / n;
        }
        int[][] arrays = new int[m][n];
        int key = N - 1;

        for (int i = 0; i < (n + 1) / 2; i++) {
            for (int up = i; up < n - i; up++) {
                arrays[i][up] = array[key];
                key--;
            }

            for (int right =  i + 1; right < m - i; right++) {
                arrays[right][n - 1 - i] = array[key];
                key--;
            }

            for (int down = n - 2 - i; down >= i; down--) {
                arrays[m - 1 - i][down] = array[key];
                key--;
            }

            for (int left = m - 2 - i; left >= i + 1; left--) {
                arrays[left][i] = array[key];
                key--;
            }
        }
        for (int i = 0; i < m; i++) {
            int j = 0;
            for ( ; j < n - 1; j++) {
                System.out.print(arrays[i][j] + " ");
            }
            System.out.println(arrays[i][j]);
        }
    }

    @Test
    public void test1(){
        int N = 17;

        int m, n;
        n = (int) Math.sqrt(N);
        m = N / n;
        while (m * n != N) {
            // d
            n = n - 1;
            m = N / n;
        }

        System.out.println(m + " " + n);

    }
}
